package com.jjkj.validator;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.validate.Validator;
import com.jjkj.base.common.R;
import com.jjkj.domain.dto.LoginUserDto;
import io.jboot.web.controller.JbootController;

public class LoginValidator extends Validator {

    @Override
    protected void validate(Controller c) {
        setRet(Ret.create());

        LoginUserDto loginUserDto = ((JbootController)c).getRawObject(LoginUserDto.class);

        if(loginUserDto == null) {
            loginUserDto = new LoginUserDto();
        }

        if(StrKit.isBlank(loginUserDto.getUsername())) {
            addError("username", "用户名不能为空");
        }

        if(StrKit.isBlank(loginUserDto.getPassword())) {
            addError("password", "密码不能为空");
        }
    }

    @Override
    protected void handleError(Controller c) {
        c.renderJson(R.error("校验失败", getRet()));
    }
}
